<?php
require_once 'library/Database.php';
$forecast = new Database();
Session::checkLogin();
$errors = array();
$message = '';
if (isset($_POST['addTrendAnalysis'])) {
    $isValidated = $forecast->validate($_POST);
    if ($isValidated === true) {
        unset($_POST['addTrendAnalysis']);
        if ($forecast->save('trendanalysis', $_POST)) {
            $message = 'Data successfully saved';
        }
    }
    else {
        $errors = $isValidated;
    }

}
if (isset($_POST['addForecast'])) {

    $isValidated = $forecast->validate($_POST);
    if ($isValidated === true) {
        unset($_POST['addForecast']);
        $_POST['total'] = intval($_POST['graduating_male_students']) + intval($_POST['graduating_female_students']);

        if ($forecast->save('forecasttbl', $_POST)) {
            $message = 'Data successfully saved';
        }
        else {
            $errors = $isValidated;
        }
    }
}

$trendAnalysis = $forecast->getTrendAnalysis();
$leavers = array();
$appliedMales = array();
$appliedFemales = array();
$admitted = array();
$came = array();

$forecastData = $forecast->getForecastData();
$continuing = array();
$graduatingMales = array();
$graduatingFemales = array();
$calculatedForecast = array();
?>
<!DOCTYPE html>
<!--[if IE 8]>
<html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js" lang="en"> <!--<![endif]-->

<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width"/>
    <title>forecasting enrollment system</title>

    <link rel="stylesheet" href="css/normalize.css"/>

    <link rel="stylesheet" href="css/foundation.css"/>
    <link rel="stylesheet" href="css/overrides.css"/>

    <script src="js/vendor/custom.modernizr.js"></script>

</head>
<body>

<div class="row">
    <div class="large-12 columns text-center">
        <div class="header">
            <img src="img/logo.png" alt="Logo">

            <p class="logout"><a href="?logout=1">Logout</a></p>
        </div>
        <h3>Welcome to the forecasting enrollment system <?php echo Session::get('username'); ?>!</h3>
        <?php if (!empty($errors)) { ?>
            <div class="alert-box alert radius">
                <?php foreach ($errors as $key => $error) { ?>
                    <?php echo $error ?><br/>
                <?php } ?>
                <a href="" class="close">×</a>
            </div>
        <?php } ?>

        <?php if (!empty($message)) { ?>
            <div class="alert-box success radius">
                <?php echo $message; ?>
                <a href="" class="close">×</a>
            </div>
        <?php } ?>
        <hr/>

        <div class="section-container tabs" data-section="tabs">

            <section class="section">
                <p class="title"><a href="#">Trend Analysis</a></p>

                <div class="content">


                    <div class="row">
                        <div class="large-3 columns">

                            <form action="dashboard.php" method="post" class="trend-analysis-form">

                                <label>Year</label>
                                <input type="text" name="year" required="required" placeholder="Enter Year">

                                <label>Number of students that left S.S</label>
                                <input type="number" required="required" name="Number_of_students_that_left_S_S"
                                       placeholder="Enter Number" required="required">

                                <label>Number of male students that applied for VVU</label>
                                <input type="number" required="required"
                                       name="Number_of_male_students_that_applied_for_VVU" placeholder="Enter Number"
                                       required="required">

                                <label>Number of female students that applied for VVU</label>
                                <input type="number" required="required"
                                       name="Number_of_female_students_that_applied_for_VVU" placeholder="Enter Number"
                                       required="required">

                                <label>Number of students admitted</label>
                                <input type="number" required="required" name="Number_of_students_admitted"
                                       placeholder="Enter Number" required="required">

                                <label>Number of students that came to VVU</label>
                                <input type="number" required="required" name="Number_of_students_that_came_to_VVU"
                                       placeholder="Enter Number" required="required">

                                <input type="submit" name="addTrendAnalysis" value="Save" class="button">
                            </form>
                        </div>

                        <div class="large-9 columns">

                            <?php if ($trendAnalysis) { ?>
                                <table class="trend-analysis" style="width: 100%">
                                    <caption>Trend Analysis</caption>
                                    <thead>
                                    <tr>
                                        <th></th>
                                        <?php while ($data = mysql_fetch_object($trendAnalysis)) { ?>
                                            <?php array_push($leavers, $data->Number_of_students_that_left_S_S); ?>
                                            <?php array_push($appliedMales, $data->Number_of_male_students_that_applied_for_VVU); ?>
                                            <?php array_push($appliedFemales, $data->Number_of_female_students_that_applied_for_VVU); ?>
                                            <?php array_push($admitted, $data->Number_of_students_admitted); ?>
                                            <?php array_push($came, $data->Number_of_students_that_came_to_VVU); ?>
                                            <th><?php echo $data->year; ?></th>
                                        <?php } ?>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr>
                                        <th>Leavers</th>
                                        <?php foreach ($leavers as $k => $leaver) { ?>
                                            <td><?php echo $leaver; ?></td>
                                        <?php } ?>
                                    </tr>
                                    <tr>
                                        <th>Applied Males</th>
                                        <?php foreach ($appliedMales as $k => $male) { ?>
                                            <td><?php echo $male; ?></td>
                                        <?php } ?>
                                    </tr>
                                    <tr>
                                        <th>Applied females</th>
                                        <?php foreach ($appliedFemales as $k => $female) { ?>
                                            <td><?php echo $female ?></td>
                                        <?php } ?>
                                    </tr>
                                    <tr>
                                        <th>Admitted</th>
                                        <?php foreach ($admitted as $k => $student) { ?>
                                            <td><?php echo $student; ?></td>
                                        <?php } ?>
                                    </tr>
                                    <tr>
                                        <th>Came</th>
                                        <?php foreach ($came as $k => $student) { ?>
                                            <td><?php echo $student; ?></td>
                                        <?php } ?>
                                    </tr>

                                    </tbody>
                                </table>
                            <?php } ?>
                        </div>
                    </div>


                </div>
            </section>
            <section class="section">
                <p class="title"><a href="#">Forecast</a></p>

                <div class="content">

                    <div class="row">
                        <div class="large-3 columns">
                            <form action="dashboard.php" method="post" class="forecast-form">
                                <label>Year</label>
                                <input type="text" name="year" placeholder="Enter Year">

                                <label>Continuing Students</label>
                                <input type="number" name="continuing_students" placeholder="Enter Number" required="required">

                                <label>Graduating Male Students</label>
                                <input type="number" name="graduating_male_students" placeholder="Enter Number" required="required">

                                <label>Graduating Female Students</label>
                                <input type="number" name="graduating_female_students" placeholder="Enter Number" required="required">

                                <input type="submit" name="addForecast" value="Forecast" class="button">
                            </form>
                        </div>

                        <div class="large-9 columns forecast-tables-holder">
                            <?php if ($forecastData) { ?>
                            <table class="forecast-table" style="width: 100%">
                                <caption>Forecast</caption>
                                <thead>
                                <tr>
                                    <th></th>
                                    <?php while ($data = mysql_fetch_object($forecastData)) { ?>

                                        <?php $formular = $forecast->getGraduatingStudentsByYear($data->year);
                                        $calc = ($formular->Number_of_students_that_came_to_VVU + $data->continuing_students) - ($data->graduating_male_students + $data->graduating_female_students); ?>


                                        <?php array_push($continuing, $data->continuing_students); ?>
                                        <?php array_push($graduatingMales, $data->graduating_male_students); ?>
                                        <?php array_push($graduatingFemales, $data->graduating_female_students); ?>
                                        <?php array_push($calculatedForecast, $calc); ?>

                                        <th><?php echo $data->year; ?></th>
                                    <?php } ?>
                                </tr>
                                </thead>
                                <tbody>
                                <tr>
                                    <th>Continuing</th>
                                    <?php foreach ($continuing as $k => $student) { ?>
                                    <td><?php echo $student ?></td>
                                    <?php } ?>
                                </tr>
                                <tr>
                                    <th>Graduating Males</th>
                                    <?php foreach ($graduatingMales as $k => $student) { ?>
                                    <td><?php echo $student; ?></td>
                                    <?php } ?>
                                </tr>

                                <tr>
                                    <th>Graduating Females</th>
                                    <?php foreach ($graduatingFemales as $k => $student) { ?>
                                    <td><?php echo $student; ?></td>
                                    <?php } ?>
                                </tr>

                                <tr>
                                    <th>Forecast</th>
                                    <?php foreach ($calculatedForecast as $k => $f) { ?>
                                    <td><?php echo $f; ?></td>
                                    <?php } ?>
                                </tr>

                                </tbody>
                            </table>
                            <?php } ?>
                        </div>

                    </div>

                </div>
            </section>
        </div>

    </div>
</div>

<div class="row">
    <div class="large-8 columns">


    </div>

    <div class="large-4 columns">

    </div>
</div>

<!--<script>-->
<!--    document.write('<script src=' +-->
<!--        ('__proto__' in {} ? 'js/vendor/zepto' : 'js/vendor/jquery') +-->
<!--        '.js><\/script>')-->
<!--</script>-->
<script src="plugins/jsapi.js" type="text/javascript"></script>
<script src="js/vendor/jquery-1.9.1.min.js"></script>
<script src="plugins/jquery.gvChart-1.1.min.js"></script>
<script src="js/foundation.min.js"></script>
<script src="js/Forecast.js"></script>

<script>

    $(document).foundation();
    try {
        gvChartInit();
    } catch (e) {
        console.log('Yawa oo: ', e.message);
    }
</script>
</body>
</html>
